package org.example.controller;


import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.example.common.R;
import org.example.domain.SysRole;
import org.example.dto.RoleDto;
import org.example.listener.SysRoleListener;
import org.example.service.SysRoleService;
import org.example.util.Result;
import org.spring.kun.common.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = {"角色接口"})
@RestController
@RequestMapping("sysRole")
public class SysRoleController {
    @Autowired
    SysRoleService sysRoleService;
//
//    @Autowired
//    PdfService pdfService;

    @ApiOperation(value = "角色查询接口")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功"),
            @ApiResponse(code = 600,message = "失败")
    })
    @RequestMapping("rolesList")
    public Result rolesList(){
        List<SysRole> list = sysRoleService.rolesList();
        return Result.success(list);
    }
    @RequestMapping("roleList")
    public R roleList(RoleDto roleDto) {
        PageHelper.startPage(roleDto.getPageNum(), roleDto.getPageSize());
        List<SysRole> list = sysRoleService.roleList(roleDto);
        PageInfo<SysRole> info = new PageInfo<>(list);
        return R.successed(info);
    }

    @RequestMapping("roleDels")
    public R dels(Long[] id) {
        System.err.println("id::" + Arrays.asList(id));
        boolean b = sysRoleService.roleDels(Arrays.asList(id));
        return b?R.successed(null):R.failed(null);
    }

    @RequestMapping("roleSave")
    public R save(SysRole sysRole) {
        sysRoleService.save(sysRole);
        return R.successed(null);
    }

    @RequestMapping("listById")
    public R listById(Long id) {
        SysRole sysRole = sysRoleService.listById(id);
        return R.successed(sysRole);
    }

    @RequestMapping("roleUpdate")
    public R Update(SysRole sysRole) {
        sysRoleService.update(sysRole);
        return R.successed(null);
    }

//
//    @RequestMapping("rolePdf")
//    public R pdf() throws Exception {
//        List<SysRole> list = sysRoleService.list();
//        pdfService.export(list, "role.pdf", "角色数据", SysRole.class);
//        SignImageUtil.create("E:/keystore/pdf/sign/role.pdf", "E:/keystore/pdf/sign/rolesign.pdf");
//        WaterMarkUtil.addWaterMark("E:/keystore/pdf/sign/rolesign.pdf", "E:/keystore/pdf/sign/rolesignwater.pdf", "张佳尧", 3);
//        return R.successed(null);
//    }

    @RequestMapping("roleExcel")
    public void excel(HttpServletResponse response) {
        List<SysRole> list = sysRoleService.list();
        ExcelUtils.exportExcel(list, "角色数据", "role", response);
    }

    @RequestMapping("getrole")
    public R getrole(){
        List<SysRole> list = sysRoleService.getrole();
        return R.successed(list);
    }

    @GetMapping("down")
    public void download(HttpServletResponse response) throws IOException {
        List<SysRole> list = sysRoleService.list();

        String simpleName = SysRole.class.getSimpleName();
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        simpleName = URLEncoder.encode(simpleName, "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + simpleName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), SysRole.class).sheet("模板").doWrite(list);
    }

    @PostMapping("upload")
    @ResponseBody
    public Map<String,Object> upload(MultipartFile file) throws IOException {
        Map<String,Object> map = new HashMap<>();
        try {
            map.put("code",200);
            EasyExcel.read(file.getInputStream(), SysRole.class, new SysRoleListener(sysRoleService)).sheet().doRead();
        }catch (IOException e){
            map.put("code",500);
            e.printStackTrace();
        }

        return map;
    }
}
